#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cou.h"

struct Cou* creatList1()
{
	struct Cou* listHeadNode = (struct Cou*)malloc(sizeof(struct Cou));
	listHeadNode->next=NULL;
	return listHeadNode; 
}
struct Cou* creatNode1(struct course data)
{
	struct Cou* newNode = (struct Cou*)malloc(sizeof(struct Cou));
	newNode->data=data;
	newNode->next=NULL;
	return newNode; 
}
//查找
struct Cou* searchNodeByAppoincNum(struct Cou* listHeadNode,char *num)
{
	struct Cou* pMove=listHeadNode->next;
	if(pMove==NULL)
		return pMove;
	else
	{
		while(strcmp(pMove->data.num,num))
		{
			pMove=pMove->next;
			if(pMove==NULL)
				break;
		}
		return pMove;
	}
}
void printNode1(struct Cou* curNode1)
{
	printf("课程编号\t课程名称\t课程性质\t授课学时\t学分\t课程已选人数\t课程人数上限\n");
	printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",curNode1->data.num,curNode1->data.name,curNode1->data.nature,curNode1->data.time,curNode1->data.score,curNode1->data.peo,curNode1->data.peom);
}
//修改

//删除
void deleteNodeByAppoincName(struct Cou* listHeadNode,char *name)
{
	struct Cou* posFrontNode=listHeadNode;
	struct Cou* posNode=listHeadNode->next;
	if(posNode==NULL)
	{
		printf("无相关内容,无法删除\n");
		return;
	}else{
		while(strcmp(posNode->data.name,name))
		{	
			posFrontNode=posNode;
			posNode=posFrontNode->next;
			if(posNode==NULL)
			{
				printf("无相关内容,无法删除\n");
				return;
			}	
		}
		posFrontNode->next=posNode->next;
		free(posNode);
	}
} 
//插入 
void insertNodeByHead1(struct Cou* listHeadNode, struct course data)
{
	struct Cou* newNode=creatNode1(data);
	newNode->next=listHeadNode->next;
	listHeadNode->next=newNode;
}
//打印 
void printList1(struct Cou* listHeadNode)
{
	struct Cou* pMove =listHeadNode->next;
	printf("课程编号\t课程名称\t课程性质\t授课学时\t学分\t课程已选人数\t课程人数上限\n");
	while(pMove)
	{
		printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",pMove->data.num,pMove->data.name,pMove->data.nature,pMove->data.time,pMove->data.score,pMove->data.peo,pMove->data.peom);
		pMove=pMove->next; 
	}
	printf("\n");
}

void readInfoFromFile2(char *fileName,struct Cou* listHeadNode)
{
	FILE *fp=fopen(fileName,"r");
	if(fp==NULL)
	{
		fp=fopen(fileName,"w");	
	}	
	struct course tempData;
	while(fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",tempData.num,tempData.name,tempData.nature,tempData.time,tempData.score,tempData.peo,tempData.peom)!=EOF)
	{
		insertNodeByHead1(listHeadNode,tempData);
		memset(&tempData,0,sizeof(tempData));
	}
	fclose(fp);
}
void saveInfoToFile1(char  *fileName,struct Cou* listHeadNode)
{
	FILE *fp=fopen(fileName,"w");
	struct Cou* pMove =listHeadNode->next;
	while(pMove)
	{
		fprintf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",pMove->data.num,pMove->data.name,pMove->data.nature,pMove->data.time,pMove->data.score,pMove->data.peo,pMove->data.peom);
		pMove=pMove->next; 
	}
	fclose(fp);
}
